package definite.Guide.Book.ListModel;

import java.awt.EventQueue;
import java.util.ArrayList;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.JFrame;

public class ArrayListComboBoxModel extends AbstractListModel implements
		ComboBoxModel {

	// field
	private ArrayList arrayList;
	private Object selectedItem;

	// constructor
	public ArrayListComboBoxModel(ArrayList list) {
		this.arrayList = list;
	}

	@Override
	public int getSize() {
		return arrayList.size();
	}

	@Override
	public Object getElementAt(int index) {
		return arrayList.get(index);
	}

	@Override
	public void setSelectedItem(Object anItem) {
		this.selectedItem = anItem;
	}

	@Override
	public Object getSelectedItem() {
		return selectedItem;
	}

	public static void main(String[] args) {
		Runnable r = new Runnable() {

			@Override
			public void run() {
				JFrame.setDefaultLookAndFeelDecorated(true);
				JFrame frame = new JFrame("Demo ComboBox");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				frame.setSize(300, 200);
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		};

		EventQueue.invokeLater(r);
	}
}
